<?php
/**
 * File which holds the formhandler
 *
 * @author Xander Hoogland <home@xanland.nl>
 * @package classes
 */

/**
 * Formhandler to easily create forms
 *
 * @author Xander Hoogland <home@xanland.nl>
 * @package classes
 */
class cForm
{
	/**
	 * Array with all the elements defined
	 *
	 * @access private
	 * @var array
	 */
	private $_aElements = array ();

	/**
	 * Set a name for the form
	 *
	 * @access private
	 * @var string
	 */
	private $_sName;

	/**
	 * Set a classname for the form
	 *
	 * @access private
	 * @var string
	 */
	private $_sClass;

	/**
	 * Constructor which sets the class for the form
	 * for easy styling
	 *
	 * @access public
	 */
	public function __construct ($sName, $sClass)
	{
		$this -> _sName = $sName;

		$this -> _sClass = $sClass;
	}

	/**
	 * Add a button to the form
	 *
	 * @access public
	 * @param string $sName
	 * @param string $sType
	 * @param string $sValue
	 * @return \cForm
	 */
	public function addButton ($sName, $sType = 'submit')
	{
		$this -> _aElements ['button'] [$sName] = '<label>&nbsp;</label>&nbsp;<button name="' . $this -> _sName . '" type="' . $sType . '">' . $sName . '</button>';
		return $this;
	}

	/**
	 * Add an input-field for filling
	 *
	 * @access public
	 * @param string $sName
	 * @param string $sType
	 * @param string $sValue
	 * @param string $sPlaceholder
	 * @param boolean $bRequired
	 * @return \cForm
	 */
	public function addInput ($sName, $sType, $sSize = NULL, $bRequired = NULL, $sValue = NULL, $sPlaceholder = NULL)
	{
		$sSize			= (is_numeric ($sSize)) ? 'size="' . $sSize . '"' : '';
		$bRequired		= ($bRequired === true)	? 'required="required"' : '';
		$sPlaceholder	= (!empty ($sPlaceholder)) ? 'placeholder="' . $sPlaceholder . '"' : '';
		$sHtml = '<label>' . ucfirst ($sName) . ':</label>&nbsp;';
		$sHtml .= '<input autocomplete="on" maxlength="255" name="' . $sName . '" ' . $sPlaceholder . ' ' . $bRequired . ' ' . $sSize . ' type="' . $sType . '" /><br />' . PHP_EOL;

		if ($sType == 'hidden')
		{
			$sHtml .= '<input type="hidden" name="' . $sName . '" value="' . $sValue . '" />';
		}

		$this -> _aElements ['input'] [$sName] = $sHtml;
		return $this;

	}

	/**
	 * Add an textarea for (huge) textfields
	 *
	 * @access public
	 * @param string $sName
	 * @param string $sValue
	 * @param string $sPlaceholder
	 * @return \cForm
	 */
	public function addTextarea ($sName, $sValue, $sPlaceholder = NULL)
	{
		$this -> _aElements ['textarea'] [$sName] = '<label>' . ucfirst ($sName) . '</label>:&nbsp;<textarea cols="25" name="' . $sName . '" placeholder="' . $sPlaceholder . '" rows="5">' . $sValue . '</textarea><br />' . PHP_EOL;
		return $this;
	}

	public function isIdentical ()
	{

	}

	/**
	 * Just an easy check to see if the form is posted
	 *
	 * @return boolean
	 */
	public function isPosted ()
	{
		if (strtolower ($_SERVER ['REQUEST_METHOD']) == strtolower ('POST') && isset ($_POST [ $this -> _sName ]))
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	/**
	 * Return the whole form
	 *
	 * @access public
	 * @return string
	 */
	public function	output ()
	{
		$sHtml = '<form class="' . $this -> _sClass . '" name="' . $this -> _sName . '" method="POST">' . PHP_EOL;
		foreach ($this -> _aElements as $a)
		{
			if (is_array ($a))
			{
				foreach ($a as $b)
				{
					$sHtml .= $b;
				}
			}
			else
			{
				$sHtml .= $a;
			}
		}
		$sHtml .= '</form>';
		return $sHtml;
	}
}